<template>
  <div class="menu">
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess" row-key="id">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'removewatermark:add'">
          新增
        </a-button>
        <a-button
          type="primary"
          :disabled="!selectKey.length"
          v-auth="'removewatermark:delete'"
          danger
          @click="handleBatchDelete"
        >
          删除
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                onClick: handleLook.bind(null, record),
                label: '查看',
              },
              {
                onClick: handleEdit.bind(null, record),
                label: '编辑',
                auth: 'removewatermark:edit',
              },
              {
                onClick: handleDown.bind(null, record),
                label: '下载',
              },
              {
                onClick: handleDelete.bind(null, record),
                label: '删除',
                auth: 'removewatermark:delete',
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';

  import { useModal } from '@/components/Modal';
  import MenuModal from './Modal.vue';
  import { columns, searchFormSchema } from './index.data';

  import { useMessage } from '@/hooks/web/useMessage';

  import * as api from '@/api/removeWatermark/removeWatermark';
  import { ref } from 'vue';
  import v_dialog from '@/components/rw/videoDialog/index.js';

  defineOptions({ name: 'MenuManagement' });

  const { createConfirm, createMessage: msg } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const selectKey = ref([]);
  const [registerTable, { reload }] = useTable({
    api: api.getList,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    isTreeTable: true,
    pagination: true,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    fetchSetting: {
      // 传给后台的当前页字段
      pageField: 'pageIndex',
      // 传给后台的每页显示多少条的字段
      sizeField: 'pageSize',
      // 接口返回表格数据的字段
      listField: 'data.entities',
      // 接口返回表格总数的字段
      totalField: 'data.totalCount',
    },
    rowSelection: {
      type: 'checkbox',
      onChange: (e) => {
        selectKey.value = e;
      },
    },
    actionColumn: {
      width: 140,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: 'right',
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
      isLook: false,
    });
  }

  function handleBatchDelete(record: Recordable) {
    createConfirm({
      title: '删除',
      content: '是否要批量删除',
      iconType: 'warning',
      onOk: async () => {
        await api.deletedata(selectKey.value);
        msg.success('删除成功');
        selectKey.value = [];
        reload();
      },
    });
  }
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      isLook: false,
    });
  }
  function handleDown(record: Recordable) {
    console.log('videourl', record);
    if (!record.afterUrl) {
      return;
    }

    const v_dialogs = v_dialog.newInstance({ deviceId: record.afterUrl, videoPerson: true });

    if (!v_dialogs) return msg.error('无法播放');

    v_dialogs.diagram.init(record);

    fetch(record.afterUrl)
      .then((res) => res.blob())
      .then((blob) => {
        const a = document.createElement('a');
        document.body.appendChild(a);
        a.style.display = 'none';
        const url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = '视频.mp4';
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
      });
  }
  function handleLook(record: Recordable) {
    openModal(
      true,
      {
        record,
        isUpdate: true,
        isLook: true,
      },
      true,
    );
  }

  function handleDelete(record: Recordable) {
    createConfirm({
      title: '删除',
      content: '确定要删除吗？删除后不可恢复',
      iconType: 'warning',
      onOk: async () => {
        await api.deletedata([record.id]);
        selectKey.value = [];
        reload();
      },
    });
  }

  function handleSuccess() {
    reload();
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
  }
</script>
